package com.gdpa.controller;


import com.gdpa.entity.DistrArea;
import com.gdpa.entity.vo.ResponseData;
import com.gdpa.service.IDistrAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * <p>
 * 分销区域表 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2018-12-19
 */
@RestController
@RequestMapping("/distr-area")
public class DistrAreaController {
    @Autowired
    IDistrAreaService iDistrAreaService;

    @RequestMapping("/addArea")
    public String addArea(HttpServletRequest request){
        String data = request.getParameter("data");
        iDistrAreaService.addArea(data);
        System.out.print("收到数据："+data);
        return "ture";
    }
    @RequestMapping("/checkArea")
    public String checkArea(HttpServletRequest request){
        String data = request.getParameter("data");
        boolean b = iDistrAreaService.AreaIsExist(data);
        System.out.println("查询结果："+b);
        if (b){
            return "ture";
        }else{
            return "fa";
        }
    }

    /**
     * 模糊查询区域
     * @param distrAreaName
     * @param request
     * @return
     */
    @RequestMapping("/findArea")
    public ModelAndView findArea(String distrAreaName,HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        System.out.println("收到的数据："+distrAreaName);
        List<DistrArea> distrArea = iDistrAreaService.findArea(distrAreaName);
        request.setAttribute("list",distrArea);
        ModelAndView mv = new ModelAndView("distr_area/area_manage");
        return mv;
    }

    /**
     * 查询所有区域
     * @param request
     * @return
     */
    @RequestMapping("/allArea")
    public ModelAndView allArea(HttpServletRequest request){
        List<DistrArea> distrArea = iDistrAreaService.findArea("");
        request.setAttribute("list",distrArea);
        ModelAndView mv = new ModelAndView("distr_area/area_manage");
        return mv;
    }
    @RequestMapping("/deleteArea")
    public ModelAndView deleteArea(String data){
        System.out.println("收到的数据："+data);
        if (data != null){
            int n = iDistrAreaService.deleteArea(data);
            if (n > 0){
                System.out.println("删除成功");
            }
        }
        ModelAndView mv = new ModelAndView("redirect:allArea");
        return mv;
    }
    @RequestMapping("/reviseArea")
    public String reviseArea(String distrAreaName,String oldName) throws UnsupportedEncodingException {
//        request.setCharacterEncoding("utf-8");
//        String distrAreaName = request.getParameter("distrAreaName");
//        String oldName = request.getParameter("oldName");
        System.out.println("收到的数据："+distrAreaName);
        if(distrAreaName != null){
            int n = iDistrAreaService.updateArea(distrAreaName,oldName);
            if (n > 0){
                return "ture";
            }
        }
        return "";
    }
}
